// Created By ����&��Ĭ�Ĵ���
// Last modified date: 2016-01-24
// Location: Chengdu

// TODO: Date/time switch
// TODO: Automatically turn off LED

#include "stm32f10x.h"
#include "HV57708.h"
#include "ds3231.h"
#include <stdbool.h>
#include "delay.h"
#include "usart3.h"
#include "SHT2X.h"
#include "timer.h"

u8 showMode = 0;
u8 dis_data[10] = {0};
DeviceConf deviceConf = {0};
extern Time_Typedef TimeValue;
extern u8 Display_Time[8];
extern unsigned char USART3_RX_BUF[USART3_MAX_RECV_LEN];

static u8 CalcXorByte(u8 *data, u8 len)
{
	u8 result = 0;
	int i = 0;
	for (; i < len; i++)
	{
		result = result ^ *(data + i);
	}
	return result;
}

void SyncTime(u8 *time)
{
	char strTmp[7] = {0};
	sprintf(strTmp, "%02d%02d%02d", time[3], time[4], time[5]);
	dis_data[0] = strTmp[1] - '0';
	dis_data[1] = strTmp[0] - '0';
	dis_data[2] = strTmp[2] - '0';
	dis_data[3] = strTmp[3] - '0';
	dis_data[4] = strTmp[5] - '0';
	dis_data[5] = strTmp[4] - '0';
	TimeValue.hour = (dis_data[1] << 4) | dis_data[0];
	TimeValue.minute = (dis_data[2] << 4) | dis_data[3];
	TimeValue.second = (dis_data[5] << 4) | dis_data[4];

	DS3231_Time_Init(&TimeValue);
}

void SyncConf(u8 *recData)
{
	memcpy(&deviceConf, recData + 3, sizeof(DeviceConf));
}

void DataParse()
{
	u8 *recData = (u8 *)malloc((USART3_RX_STA & 0x00FF) + 3);
	u8 len = (USART3_RX_STA & 0x3F00) >> 8, xorByte = 0;
	DataPack *dataPack = 0;
	recData[0] = DATA_HEAD;
	recData[1] = len;
	memcpy(recData + 2, USART3_RX_BUF, len);
	xorByte = CalcXorByte(recData, len + 1);
	if (xorByte != recData[len + 1])
	{
		if (recData)
			free(recData);
		return;
	}

	dataPack = (DataPack *)recData;
	switch (dataPack->cmd)
	{
	case CMD_SET_TIME:
		SyncTime(recData);
		break;
	case CMD_SET_CONF:
		SyncConf(recData);
		break;
	default:
		break;
	}
	if (recData)
		free(recData);
}

void SleepModeCheck()
{
	u32 timeNow = (((TimeValue.hour >> 4) * 10) + (TimeValue.hour & 0x0f)) * 3600 + (((TimeValue.minute >> 4) * 10) + (TimeValue.minute & 0x0f)) * 60 + (((TimeValue.second >> 4) * 10) + (TimeValue.second & 0x0f));
	u32 timeStart = deviceConf.startSleepTime_;
	u32 timeEnd = deviceConf.endSleepTime_;
	if (timeStart > timeEnd)
		timeEnd += 60 * 60 * 24;
	if (timeStart <= timeNow && timeEnd >= timeNow)
		HV57708_DISABLE;
	else
		HV57708_ENABLE;
}

int main(void)
{
	// u32 i,j;
	int cnt = 0;
	delay_init(); //��ʱ������ʼ��
	usart3_init(115200);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //����NVIC�жϷ���2:2λ��ռ���ȼ���2λ��Ӧ���ȼ�
	HV57708_Init(HV57708_MODE_NORMAL);
	IIC_GPIO_Config();
	SHT20_GPIO_INIT();
	HV57708_ENABLE;
	HV57708_DISABLE;
	HV57708_ENABLE;
	Time_Handle();
	TIM3_Int_Init(7199, 4999);
	for (; cnt < 10; cnt++)
	{
		memset(dis_data, cnt, 6);
		SetDisplay(dis_data);
		delay_ms(100);
	}
	while (1)
	{
		char strTmp[7] = {0};
		if (USART3_RX_STA & 0x8000)
		{
			DataParse();
			memset(USART3_RX_BUF, 0, sizeof(USART3_RX_BUF));
			USART3_RX_STA = 0;
		}
		Time_Handle();
		SleepModeCheck();
		dis_data[0] = Display_Time[1] - '0';
		dis_data[1] = Display_Time[0] - '0';
		dis_data[2] = Display_Time[3] - '0';
		dis_data[3] = Display_Time[4] - '0';
		dis_data[4] = Display_Time[7] - '0';
		dis_data[5] = Display_Time[6] - '0';
		if (deviceConf.envDataEnable_)
		{
			if (showMode)
			{
				float temp = SHT20_Measure('T'), humi = SHT20_Measure('H');
				dis_data[0] = (int)temp % 10;
				dis_data[1] = (int)temp / 10;
				dis_data[2] = 0;
				dis_data[3] = 0;
				dis_data[4] = (int)humi % 10;
				dis_data[5] = (int)humi / 10;
				
			}
		}
		SetDisplay(dis_data);
		delay_ms(20);
	}
}
